package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

import artid.com.arttogoa.R;
import artid.com.arttogoa.app.ArtWorkApp;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.ArtistWorkInfoParse;
import artid.com.arttogoa.parser.OrderInfoParser;
import artid.com.arttogoa.util.AlertUtils;
import artid.com.arttogoa.util.AppUtil;
import artid.com.arttogoa.util.NetUtils;
import artid.com.arttogoa.util.SendHxMessage_Utils;
import artid.com.arttogoa.util.VeDate;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：OrderInfo_Activity   
 * 类描述   ：订单详情   
 * 创建人   ：耿卫斌   
 * 创建时间：2016-7-26 下午4:29:29   
 *
 */
public class OrderInfo_Activity extends BaseActivity implements OnClickListener{
	private TextView orderState,nickName,contactBuyer,title,isguarantee,price,addressee,recipientAddress_tv,deliverGoodsAddress_tv,distributionMode,express,courierNumber,
	orderNum,placeAnOrderTime,distributionTime,dealTime,closeTime,paymentTime,contactUs,distributionBtn;
	private ImageView img;
	private LinearLayout hairExpressMessageLL,deliverGoodsAddressLL;
	private int  whichActivity=0;//(0：全部  1：待发货 2：买家待付款 3：已发货 4：已出售 5：已关闭)                     
	private List<OrderInfoParser> list;
	private String order_id;//商品ID，从列表传过来的
	private OrderInfoParser mOrderInfoParser;//订单详情解析后的实体数据
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_orderinfo);
		setContent(this, getIntent());
		initView();
	}

	private void initView(){
		mTittle.setText("订单详情");
		setBack();
		// 注册当前广播
		IntentFilter filter = new IntentFilter("com.OrderInfo_Activity.receiver");
		registerReceiver(broadcastReceiver, filter);
		whichActivity=getIntent().getIntExtra("whichActivity", 0);
		order_id=getIntent().getStringExtra("order_id");
		list=new ArrayList<OrderInfoParser>();
		orderState=(TextView) findViewById(R.id.orderinfo_orderState);
		nickName=(TextView) findViewById(R.id.orderinfo_nickName);
		contactBuyer=(TextView) findViewById(R.id.orderinfo_contactBuyer);
		img=(ImageView) findViewById(R.id.orderinfo_img);
		title=(TextView) findViewById(R.id.orderinfo_title);
		isguarantee=(TextView) findViewById(R.id.orderinfo_isguarantee);
		price=(TextView) findViewById(R.id.orderinfo_price);
		addressee=(TextView) findViewById(R.id.orderinfo_addressee);
		recipientAddress_tv=(TextView) findViewById(R.id.orderinfo_recipientAddress_tv);
		deliverGoodsAddressLL=(LinearLayout) findViewById(R.id.orderinfo_deliverGoodsAddressLL);
		deliverGoodsAddress_tv=(TextView) findViewById(R.id.orderinfo_deliverGoodsAddress_tv);
		hairExpressMessageLL=(LinearLayout) findViewById(R.id.orderinfo_hairExpressMessageLL);
		distributionMode=(TextView) findViewById(R.id.orderinfo_distributionMode);
		express=(TextView) findViewById(R.id.orderinfo_express);
		courierNumber=(TextView) findViewById(R.id.orderinfo_courierNumber);
		orderNum=(TextView) findViewById(R.id.orderinfo_orderNum);
		placeAnOrderTime=(TextView) findViewById(R.id.orderinfo_placeAnOrderTime);
		distributionTime=(TextView) findViewById(R.id.orderinfo_distributionTime);
		dealTime=(TextView) findViewById(R.id.orderinfo_dealTime);
		closeTime=(TextView) findViewById(R.id.orderinfo_closeTime);
		paymentTime=(TextView) findViewById(R.id.orderinfo_paymentTime);
		contactUs=(TextView) findViewById(R.id.orderinfo_contactUs);
		distributionBtn=(TextView) findViewById(R.id.orderinfo_distributionBtn);
		contactBuyer.setOnClickListener(this);
		courierNumber.setOnClickListener(this);
		contactUs.setOnClickListener(this);
		distributionBtn.setOnClickListener(this);
		//判断是否是推送消息进入的
		if(getIntent().getBooleanExtra("isPush", false)==true){
			ArtWorkApp.getInstance().addActivity(this);
		}else{
			showWhichActivityView();
		}
		getData();
	}

	/**
	 * 显示选择的状态布局
	 * (0：全部  1：待发货 2：买家待付款 3：已发货 4：已出售 5：已关闭)  
	 */
	private void showWhichActivityView(){
		if(whichActivity==0){
			//全部

		}else if(whichActivity==1){
			//待发货
			deliverGoodsAddressLL.setVisibility(View.GONE);
			distributionMode.setVisibility(View.GONE);
			express.setVisibility(View.GONE);
			courierNumber.setVisibility(View.GONE);
			distributionTime.setVisibility(View.GONE);
			dealTime.setVisibility(View.GONE);
			closeTime.setVisibility(View.GONE);
			distributionBtn.setVisibility(View.VISIBLE);
		}else if(whichActivity==2){
			//买家待付款
			deliverGoodsAddressLL.setVisibility(View.GONE);
			distributionMode.setVisibility(View.GONE);
			express.setVisibility(View.GONE);
			courierNumber.setVisibility(View.GONE);
			distributionTime.setVisibility(View.GONE);
			dealTime.setVisibility(View.GONE);
			closeTime.setVisibility(View.GONE);
			paymentTime.setVisibility(View.GONE);
		}else if(whichActivity==3){
			//已发货
			dealTime.setVisibility(View.GONE);
			closeTime.setVisibility(View.GONE);
			paymentTime.setVisibility(View.GONE);
		}else if(whichActivity==4){
			//已出售
			closeTime.setVisibility(View.GONE);
			paymentTime.setVisibility(View.GONE);
		}else if(whichActivity==5){
			//已关闭
			deliverGoodsAddressLL.setVisibility(View.GONE);
			distributionMode.setVisibility(View.GONE);
			express.setVisibility(View.GONE);
			courierNumber.setVisibility(View.GONE);
			distributionTime.setVisibility(View.GONE);
			dealTime.setVisibility(View.GONE);
			paymentTime.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (v.getId()) {
		case R.id.orderinfo_contactBuyer:
			//联系买家
			// 进入聊天页面
			if(mOrderInfoParser!=null){
				SendHxMessage_Utils.sendMessage(mContext,mOrderInfoParser.getBuyer_id(), mOrderInfoParser.getBuyer_nickname(),
						mOrderInfoParser.getBuyer_avatar(), mOrderInfoParser.getGoods_name(), mOrderInfoParser.getGoods_image());
			}
			break;
		case R.id.orderinfo_courierNumber:
			//查看物流信息
			if(!StringUtils.isEmpty(mOrderInfoParser.getShipping_code())){
				intent=new Intent(mContext,LogisticsInfo_Activity.class);
				intent.putExtra("shipping_code", mOrderInfoParser.getShipping_code());
				intent.putExtra("order_id", mOrderInfoParser.getOrder_id());
				startActivity(intent);
			}else{
				showToast("信息还在请求中，请稍后重试");
			}
			break;
		case R.id.orderinfo_contactUs:
			//联系我们
			AlertUtils.MakePhoneDialog(mContext,"拨打客服电话", "010-64688820", this.getWindowManager().getDefaultDisplay().getWidth());
			break;
		case R.id.orderinfo_distributionBtn:
			//我要发货
			intent= new Intent(mContext,MyDeliverGoods_Activity.class);
			intent.putExtra("order_id", order_id);
			intent.putExtra("isPush", getIntent().getBooleanExtra("isPush", false));
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	private void getData(){
		VolleyBasic.getInstance(this).post(true,false, Cache.orderDetail,
				VolleyMap.getInstance().orderDetail(order_id),
				OrderInfoParser.class, new VolleyCallback() {

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				VolleyBasic.disMissLoding();
				list=(List<OrderInfoParser>) vd.getList();
				if(list==null || list.size()==0){
					showToast("请求数据错误");
					return;
				}
				mOrderInfoParser=list.get(0);
				String order_state=mOrderInfoParser.getOrder_state();
				if("5".equals(order_state)){
					//全部

				}else if("20".equals(order_state)){
					//待发货
					orderState.setText("等待卖家发货");
				}else if("10".equals(order_state)){
					//买家待付款
					orderState.setText("买家待付款");
				}else if("30".equals(order_state)){
					//已发货
					orderState.setText("已发货");
				}else if("40".equals(order_state)){
					//已出售
					orderState.setText("交易成功");
				}else if("0".equals(order_state)){
					orderState.setText("订单已关闭");
					orderState.setBackgroundColor(Color.parseColor("#BCBCBC"));
				}
				//判断是否是推送消息进入的
				if(getIntent().getBooleanExtra("isPush", false)==true){
					if("5".equals(order_state)){
						//全部
						whichActivity=0;
					}else if("20".equals(order_state)){
						//待发货
						whichActivity=1;
					}else if("10".equals(order_state)){
						//买家待付款
						whichActivity=2;
					}else if("30".equals(order_state)){
						//已发货
						whichActivity=3;
					}else if("40".equals(order_state)){
						//已出售
						whichActivity=4;
					}else if("0".equals(order_state)){
						//已关闭
						whichActivity=5;
					}
					showWhichActivityView();
				}
				nickName.setText(mOrderInfoParser.getBuyer_nickname());
				ImageLoader.getInstance().displayImage(mOrderInfoParser.getGoods_image()
						+ "_"+ NetUtils.dip2px(mContext, 100)+"x"+NetUtils.dip2px(mContext, 100)+".png"
						, img);
				title.setText(mOrderInfoParser.getRealname()+"《"+mOrderInfoParser.getGoods_name()+"》");
				if("1".equals(mOrderInfoParser.getIs_auth())){
					//已备案
					isguarantee.setVisibility(View.VISIBLE);
				}else{
					isguarantee.setVisibility(View.GONE);
				}
				price.setText("￥"+mOrderInfoParser.getGoods_price());
				addressee.setText("收件人："+mOrderInfoParser.getTrue_name());
				recipientAddress_tv.setText(mOrderInfoParser.getAddressinfo());
				deliverGoodsAddress_tv.setText(mOrderInfoParser.getShip_address());
				distributionMode.setText("配送方式："+mOrderInfoParser.getExpressage());
				express.setText("快递公司："+mOrderInfoParser.getE_name());
				courierNumber.setText("快递单号："+mOrderInfoParser.getShipping_code());
				orderNum.setText("订单编号："+mOrderInfoParser.getOrder_sn());
				placeAnOrderTime.setText("下单时间："+ VeDate.formatUnixTime(mOrderInfoParser.getAdd_time()));
				distributionTime.setText("发货时间："+VeDate.formatUnixTime(mOrderInfoParser.getShipping_time()));
				dealTime.setText("成交时间："+VeDate.formatUnixTime(mOrderInfoParser.getFinnshed_time()));
				closeTime.setText("关闭时间："+VeDate.formatUnixTime(mOrderInfoParser.getCancel_time()));
				paymentTime.setText("付款时间："+VeDate.formatUnixTime(mOrderInfoParser.getPay_time()));
				//判断是否是推送消息进入的
				if(getIntent().getBooleanExtra("isPush", false)==true){
					//这里是为了修改推送消息状态
					getArtistWorkInfo();
					return;
				}
			}
			public void onError(VolleyError volleyError) {

			}
		});
	}

	/**
	 * 艺术家备案动态资讯数量和推送消息数量
	 */
	private void getArtistWorkInfo(){
		VolleyBasic.getInstance(this).post(false,true,Cache.artistWorkInfo,VolleyMap.getInstance().artistWorkInfo(),
				ArtistWorkInfoParse.class, new VolleyCallback() {
			@Override
			public void onSuccess(VolleyData vd) {
				Log.d("log", vd.toString());
				ArtistWorkInfoParse bean=(ArtistWorkInfoParse) vd.getData();
				if(bean!=null){
					AppUtil.getUser().setRecordnum(bean.getRecordnum());
					AppUtil.getUser().setTrendsnum(bean.getTrendsnum());
					AppUtil.getUser().setNewsnum(bean.getNewsnum());
					AppUtil.getUser().setIs_auth(bean.getIs_auth());
					MainActivity.isSystemPush=bean.getIs_pushread();
					Intent intent2 = new Intent("com.Push.receiver");  
					intent2.putExtra("UpMessageType", true);
					mContext.sendBroadcast(intent2);
				}
			}
			@Override
			public void onError(VolleyError volleyError) {

			}
		});
	}


	//广播接收器
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override  
		public void onReceive(Context context, Intent intent) {  
			String Intent_type=intent.getStringExtra("Intent_type");
			if("我要发货".equals(Intent_type)){
				finish();
			}
		}
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// 关闭广播
		unregisterReceiver(broadcastReceiver);
	}
}
